package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.IntemInformationMapper;
import com.kang.dao.ItemDetailMapper;
import com.kang.dao.PickingItemMapper;
import com.kang.dao.PickingMapper;
import com.kang.pojo.ItemDetail;
import com.kang.pojo.Picking;
import com.kang.pojo.PickingItem;
import com.kang.service.PickingService;
import com.vonechina.entity.page.ItemDetailPage;
import com.vonechina.entity.page.PickingPage;

/**
 * 领用记录服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class PickingServiceImpl implements PickingService {
	/**
	 * 领用记录持久层
	 */
	@Resource
	private PickingMapper pm;
	/**
	 * 物品批次持久层
	 */
	@Resource
	private ItemDetailMapper idm;
	/**
	 * 领用记录物品信息持久层
	 */
	@Resource
	private PickingItemMapper pim;
	/**
	 * 物品持久层
	 */
	@Resource
	private IntemInformationMapper im;

	/**
	 * 分页查询领用记录
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<Picking> listPicking(PickingPage page) {
		page.setRows(pm.countPicking(page));
		return pm.listPicking(page);
	}

	/**
	 * 新增领用记录
	 * 
	 * @param picking
	 *            领用记录实体类
	 * @return
	 */
	@Override
	public Integer insertPicking(Picking picking) {
		return pm.insertPicking(picking);
	}

	/**
	 * 生成新的领用记录编号
	 * 
	 * @return
	 */
	@Override
	public String generatePickingNumber() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = pm.selectMaxCode();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "LY" + no;
	}

	/**
	 * 分页查询可领用物品
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<ItemDetail> listItemNotIn(ItemDetailPage page) {
		page.setRows(idm.countItemNotIn(page));
		return idm.listItemNotIn(page);
	}

	/**
	 * 新增领用物品
	 * 
	 * @param item
	 *            领用物品
	 * @return
	 */
	@Override
	public Integer addPickingItem(PickingItem item) {
		ItemDetail iDetail = idm.selectById(item.getSourceID());
		item.setItemInformationID(iDetail.getIntemInformationID());
		// idm.subtractStock(item.getSourceID(), item.getNumber());
		// im.subtractStock(iDetail.getIntemInformationID(), item.getNumber());
		return pim.insertPickingItem(item);
	}

	/**
	 * 根据ID查询领用记录
	 * 
	 * @param pickingID
	 *            领用记录ID
	 * @return
	 */
	@Override
	public Picking selectPickingByID(Integer pickingID) {
		return pm.selectPickingByID(pickingID);
	}

	/**
	 * 查询领用记录下物品信息
	 * 
	 * @param pickingID
	 *            领用记录ID
	 * @return
	 */
	@Override
	public List<PickingItem> listPickingItem(Integer pickingID) {
		return pim.listPickingItem(pickingID);
	}

	/**
	 * 提交领用记录
	 * 
	 * @param pickingID
	 *            领用记录ID
	 * @param userID
	 *            用户ID
	 * @return
	 */
	@Override
	public Integer submitPicking(int pickingID, int userID) {
		Picking p = new Picking();
		p.setPickingID(pickingID);
		p.setLastPersonID(userID);
		p.setSubmitterID(userID);
		p.setStatus(1);
		Date date = new Date();
		p.setCommitTime(date);
		p.setLastProcessingTime(date);
		int a = pm.updatePicking(p);
		if (a > 0) {
			List<PickingItem> list = pim.listPickingItem(pickingID);
			for (PickingItem item : list) {
				idm.subtractStock(item.getSourceID(), item.getNumber());
				im.subtractStock(item.getItemInformationID(), item.getNumber());
			}
		}
		return a;
	}

	/**
	 * 更新领用记录
	 * 
	 * @param picking
	 *            领用记录实体类
	 * @return
	 */
	@Override
	public Integer updatePicking(Picking picking) {
		return pm.updatePicking(picking);
	}

	/**
	 * 保存领用记录下物品信息
	 * 
	 * @param picking
	 *            领用记录ID
	 * @param itemIds
	 *            领用记录物品来源ID
	 * @param pickingNumber
	 *            物品领用数量
	 * @return
	 */
	@Override
	public Integer savePickingItem(Picking picking, Integer[] itemIds, Integer[] pickingNumber) {
		pim.deleteItemNotIn(picking.getPickingID(), itemIds);
		for (int i = 0; i < itemIds.length; i++) {
			PickingItem item = pim.selectItem(picking.getPickingID(), itemIds[i]);
			if (item == null) {
				item = new PickingItem();
				item.setItemInformationID(idm.selectById(itemIds[i]).getItemInformationID());
				item.setNumber(pickingNumber[i]);
				item.setSourceID(itemIds[i]);
				item.setPickingID(picking.getPickingID());
				item.setWarehouseManagementID(picking.getWarehouseManagementID());
				pim.insertPickingItem(item);
			} else {
				item.setNumber(pickingNumber[i]);
				item.setWarehouseManagementID(picking.getWarehouseManagementID());
				pim.updateItem(item);
			}
		}
		return itemIds.length;
	}

	/**
	 * 删除领用记录
	 * 
	 * @param pickingID
	 *            领用记录ID
	 * @return
	 */
	@Override
	public Integer deletePicking(Integer pickingID) {
		int a = pm.deletePicking(pickingID);
		if (a > 0) {
			pim.deletePickingItem(pickingID);
		}
		return a;
	}

	/**
	 * 修改领料出库-物品表
	 * 
	 * @param pickingItem
	 *            领料物品实体类
	 * @return
	 */
	@Override
	public int updatePickingIteminformation(PickingItem pickingItem) {
		return pim.updatePickingIteminformation(pickingItem);
	}
}
